# Replication file for "Immigration and the Sociocultural Divide in Central and Eastern Europe: Stasis or Evolution?" by Caroline Marie Lancaster. Please contact cml93@live.unc.edu with any questions. 

# This file cleans the 2008 and 2017 EVS data and performs all analyses reported in the main text and appendices that use EVS data.

# 28 June 2021

library(readstata13)
library(dplyr)
library(magrittr)
library(ggplot2)
library(mirt)
library(mice)
library(lavaan)
library(semTools)
library(lme4)
library(lmerTest)
library(sjPlot)
library(survey)

EVS <- read.dta13('evs2017.dta')

EVS17 <- EVS[c('country', 'v24', 'v80', 'v185', 'v186', 'v187', 'v188', 'v26', 'v81', 'v151', 'v153', 'v154', 'v155', 'v156', 'v158', 'v174_cs', 'v54', 'v225', 'v226', 'v234', 'v243_edulvlb_2','v261', 'v97','gweight', 'pweight')]

colnames(EVS17) <- c('country', 'imneigh', 'imjobs', 'imtakejobs', 'imcrime','imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion', 'divorce', 'euth', 'sex', 'party', 'relig', 'gender', 'birth', 'married', 'educ','income', 'interest', 'gweight', 'pweight')

EVS17 <- subset(EVS17, country == 'Bulgaria' | country == 'Croatia'| country == 'Czech Republic' | country == 'Estonia' | country == 'Hungary' | country == 'Lithuania' | country == 'Poland' | country == 'Romania' | country == 'Slovak Republic' | country == 'Slovenia')

EVS17[,'imneigh']<- car::recode(EVS17[,'imneigh'], "'multiple answers Mail' = NA;'no follow-up' = NA;'follow-up non response' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'mentioned' = 1; 'not mentioned' = 0; 'item not included' = NA")

#reducing levels because 2008 only has three levels
EVS17[,'imjobs']<- car::recode(EVS17[,'imjobs'], "'disagree strongly' = 1;'disagree' = 1;'neither agree nor disagree' = 2; 'agree' = 3; 'agree strongly' = 3")
EVS17$imjobs <- ifelse(grepl('[0-9]', EVS17$imjobs), EVS17$imjobs, NA)

#removing NA values, which are coded negative, from imtakejobs, imcrime, imwelfare, assim
for(i in 4:7){
  EVS17[ , i] <- ifelse(EVS17[ , i] <= 0, NA, EVS17[ , i])
}

#reversing
EVS17$imtakejobs <- abs(EVS17$imtakejobs - 10) 
EVS17$imcrime <- abs(EVS17$imcrime - 10)  
EVS17$imwelfare <- abs(EVS17$imwelfare - 10)
#does not need reversing, but placing on 0-9 scale, like those above
EVS17$assim <- EVS17$assim - 1

#recoding to five categories
EVS17$imtakejobs[EVS17$imtakejobs==0] <- 0
EVS17$imtakejobs[EVS17$imtakejobs>=1 & EVS17$imtakejobs<=2] <- 1
EVS17$imtakejobs[EVS17$imtakejobs>=3 & EVS17$imtakejobs<=5] <- 2
EVS17$imtakejobs[EVS17$imtakejobs>=6 & EVS17$imtakejobs<=7] <- 3
EVS17$imtakejobs[EVS17$imtakejobs>=8 & EVS17$imtakejobs<=9] <- 4

EVS17$imcrime[EVS17$imcrime==0] <- 0
EVS17$imcrime[EVS17$imcrime>=1 & EVS17$imcrime<=2] <- 1
EVS17$imcrime[EVS17$imcrime>=3 & EVS17$imcrime<=5] <- 2
EVS17$imcrime[EVS17$imcrime>=6 & EVS17$imcrime<=7] <- 3
EVS17$imcrime[EVS17$imcrime>=8 & EVS17$imcrime<=9] <- 4

EVS17$imwelfare[EVS17$imwelfare==0] <- 0
EVS17$imwelfare[EVS17$imwelfare>=1 & EVS17$imwelfare<=2] <- 1
EVS17$imwelfare[EVS17$imwelfare>=3 & EVS17$imwelfare<=5] <- 2
EVS17$imwelfare[EVS17$imwelfare>=6 & EVS17$imwelfare<=7] <- 3
EVS17$imwelfare[EVS17$imwelfare>=8 & EVS17$imwelfare<=9] <- 4

EVS17$assim[EVS17$assim==0] <- 0
EVS17$assim[EVS17$assim>=1 & EVS17$assim<=2] <- 1
EVS17$assim[EVS17$assim>=3 & EVS17$assim<=5] <- 2
EVS17$assim[EVS17$assim>=6 & EVS17$assim<=7] <- 3
EVS17$assim[EVS17$assim>=8 & EVS17$assim<=9] <- 4

EVS17[,'gayneigh']<- car::recode(EVS17[,'gayneigh'], "'multiple answers Mail' = NA;'no follow-up' = NA;'follow-up non response' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'mentioned' = 1; 'not mentioned' = 0")

#reducing levels because 2008 only has three levels
EVS17[,'womenjobs']<- car::recode(EVS17[,'womenjobs'], "'disagree strongly' = 1;'disagree' = 1;'neither agree nor disagree' = 2; 'agree' = 3; 'agree strongly' = 3; 'multiple answers Mail' = NA;'no follow-up' = NA;'follow-up non response' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA")

#removing NA values, which are coded negative, from imtakejobs, imcrime, imwelfare, assim
for(i in 10:15){
  EVS17[ , i] <- ifelse(EVS17[ , i] <= 0, NA, EVS17[ , i])
}

#reversing
EVS17$weed <- abs(EVS17$weed - 10) 
EVS17$gay <- abs(EVS17$gay - 10)  
EVS17$abortion <- abs(EVS17$abortion - 10)
EVS17$divorce <- abs(EVS17$divorce - 10)
EVS17$euth <- abs(EVS17$euth - 10)
EVS17$sex <- abs(EVS17$sex - 10)

#recoding to five categories
EVS17$weed[EVS17$weed==0] <- 0
EVS17$weed[EVS17$weed>=1 & EVS17$weed<=2] <- 1
EVS17$weed[EVS17$weed>=3 & EVS17$weed<=5] <- 2
EVS17$weed[EVS17$weed>=6 & EVS17$weed<=7] <- 3
EVS17$weed[EVS17$weed>=8 & EVS17$weed<=9] <- 4

EVS17$gay[EVS17$gay==0] <- 0
EVS17$gay[EVS17$gay>=1 & EVS17$gay<=2] <- 1
EVS17$gay[EVS17$gay>=3 & EVS17$gay<=5] <- 2
EVS17$gay[EVS17$gay>=6 & EVS17$gay<=7] <- 3
EVS17$gay[EVS17$gay>=8 & EVS17$gay<=9] <- 4

EVS17$abortion[EVS17$abortion==0] <- 0
EVS17$abortion[EVS17$abortion>=1 & EVS17$abortion<=2] <- 1
EVS17$abortion[EVS17$abortion>=3 & EVS17$abortion<=5] <- 2
EVS17$abortion[EVS17$abortion>=6 & EVS17$abortion<=7] <- 3
EVS17$abortion[EVS17$abortion>=8 & EVS17$abortion<=9] <- 4

EVS17$divorce[EVS17$divorce==0] <- 0
EVS17$divorce[EVS17$divorce>=1 & EVS17$divorce<=2] <- 1
EVS17$divorce[EVS17$divorce>=3 & EVS17$divorce<=5] <- 2
EVS17$divorce[EVS17$divorce>=6 & EVS17$divorce<=7] <- 3
EVS17$divorce[EVS17$divorce>=8 & EVS17$divorce<=9] <- 4

EVS17$euth[EVS17$euth==0] <- 0
EVS17$euth[EVS17$euth>=1 & EVS17$euth<=2] <- 1
EVS17$euth[EVS17$euth>=3 & EVS17$euth<=5] <- 2
EVS17$euth[EVS17$euth>=6 & EVS17$euth<=7] <- 3
EVS17$euth[EVS17$euth>=8 & EVS17$euth<=9] <- 4

EVS17$sex[EVS17$sex==0] <- 0
EVS17$sex[EVS17$sex>=1 & EVS17$sex<=2] <- 1
EVS17$sex[EVS17$sex>=3 & EVS17$sex<=5] <- 2
EVS17$sex[EVS17$sex>=6 & EVS17$sex<=7] <- 3
EVS17$sex[EVS17$sex>=8 & EVS17$sex<=9] <- 4

EVS17[,'relig']<- car::recode(EVS17[,'relig'], "'never, practically never' = 1;'less often' = 2;'once a year' = 3; 'only on specific holy days' = 4; 'once a month' = 5; 'once a week' = 6; 'more than once week' = 7; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'multiple answers Mail' = NA")

EVS17[,'gender']<- car::recode(EVS17[,'gender'], "'male' = 'male';'female' = 'female';'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA")

#NA values are negative
EVS17$birth[EVS17$birth <= 0] <- NA

EVS17[,'married']<- car::recode(EVS17[,'married'], "'married' = 'married';'registered partnership' = 'married';'widowed' = 'previously'; 'divorced' = 'previously'; 'separated' = 'previously'; 'never married and never registered partnership' = 'never'; 'multiple answers Mail' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA")

EVS17$educ <- as.numeric(EVS17$educ)
EVS17$educ[EVS17$educ>=4 & EVS17$educ<=9 | EVS17$educ== 26] <- NA
EVS17$educ[EVS17$educ>=10 & EVS17$educ<=14] <- 'Less than HS'
EVS17$educ[EVS17$educ>=15 & EVS17$educ<=16] <- 'High school'
EVS17$educ[EVS17$educ>= 17 & EVS17$educ<= 20] <- 'Some college'
EVS17$educ[EVS17$educ>= 21 & EVS17$educ<= 24] <- 'College'
EVS17$educ[EVS17$educ==25] <- 'Advanced'

EVS17[,'income']<- car::recode(EVS17[,'income'], "'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'multiple answers Mail' = NA")
EVS17$income<- as.numeric(EVS17$income)

EVS17[,'interest']<- car::recode(EVS17[,'interest'], "'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'multiple answers Mail' = NA; 'very interested' = 4; 'somewhat interested' = 3; 'not very interested' = 2; 'not at all interested' = 1; 'follow-up non response' = NA; 'no follow-up' = NA")

EVS17$cohort <- NA
EVS17$cohort <- ifelse(EVS17$birth <=1939, 1, EVS17$cohort)
EVS17$cohort <- ifelse(EVS17$birth >=1940 & EVS17$birth <=1949, 2, EVS17$cohort)
EVS17$cohort <- ifelse(EVS17$birth >=1950 & EVS17$birth <=1959, 3, EVS17$cohort)
EVS17$cohort <- ifelse(EVS17$birth >=1960 & EVS17$birth <=1969, 4, EVS17$cohort)
EVS17$cohort <- ifelse(EVS17$birth >=1970 & EVS17$birth <=1979, 5, EVS17$cohort)
EVS17$cohort <- ifelse(EVS17$birth >=1980, 6, EVS17$cohort)
EVS17$cohort <- ifelse(EVS17$birth >=1990, 7, EVS17$cohort)

#####
EVS2 <- read.dta13('~/Dropbox/EVS/evs2008.dta')

EVS8 <- EVS2[c('country', 'v54','v102', 'v268', 'v270', 'v271', 'v273', 'v57', 'v103', 'v236', 'v240', 'v241', 'v242', 'v243', 'v246', 'v264', 'v265', 'v109', 'v302', 'v303', 'v313', 'v336_3', 'v353YR', 'v186', 'weight_g', 'weight_s')]

colnames(EVS8) <- c('country', 'imneigh', 'imjobs', 'imtakejobs', 'imcrime','imwelfare', 'assim', 'gayneigh', 'womenjobs', 'weed', 'gay', 'abortion', 'divorce', 'euth', 'sex', 'party','party2', 'relig', 'gender', 'birth', 'married', 'educ', 'income', 'interest',  'gweight', 'pweight')

EVS8 <- subset(EVS8, country == 'Bulgaria' | country == 'Croatia'| country == 'Czech Republic' | country == 'Estonia' | country == 'Hungary' | country == 'Lithuania' | country == 'Poland' | country == 'Romania' | country == 'Slovak Republic' | country == 'Slovenia')

EVS8[,'imneigh']<- car::recode(EVS8[,'imneigh'], "'multiple answers Mail' = NA;'no follow-up' = NA;'follow-up non response' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'mentioned' = 1; 'not mentioned' = 0")

EVS8[,'imjobs']<- car::recode(EVS8[,'imjobs'], "'disagree' = 1;'neither' = 2; 'agree' = 3")

#reversing
EVS8$imtakejobs <- abs(EVS8$imtakejobs - 10) 
EVS8$imcrime <- abs(EVS8$imcrime - 10)  
EVS8$imwelfare <- abs(EVS8$imwelfare - 10)
#does not need reversing, but placing on 0-9 scale, like those above
EVS8$assim <- EVS8$assim - 1

#recoding to five categories
EVS8$imtakejobs[EVS8$imtakejobs==0] <- 0
EVS8$imtakejobs[EVS8$imtakejobs>=1 & EVS8$imtakejobs<=2] <- 1
EVS8$imtakejobs[EVS8$imtakejobs>=3 & EVS8$imtakejobs<=5] <- 2
EVS8$imtakejobs[EVS8$imtakejobs>=6 & EVS8$imtakejobs<=7] <- 3
EVS8$imtakejobs[EVS8$imtakejobs>=8 & EVS8$imtakejobs<=9] <- 4

EVS8$imcrime[EVS8$imcrime==0] <- 0
EVS8$imcrime[EVS8$imcrime>=1 & EVS8$imcrime<=2] <- 1
EVS8$imcrime[EVS8$imcrime>=3 & EVS8$imcrime<=5] <- 2
EVS8$imcrime[EVS8$imcrime>=6 & EVS8$imcrime<=7] <- 3
EVS8$imcrime[EVS8$imcrime>=8 & EVS8$imcrime<=9] <- 4

EVS8$imwelfare[EVS8$imwelfare==0] <- 0
EVS8$imwelfare[EVS8$imwelfare>=1 & EVS8$imwelfare<=2] <- 1
EVS8$imwelfare[EVS8$imwelfare>=3 & EVS8$imwelfare<=5] <- 2
EVS8$imwelfare[EVS8$imwelfare>=6 & EVS8$imwelfare<=7] <- 3
EVS8$imwelfare[EVS8$imwelfare>=8 & EVS8$imwelfare<=9] <- 4

EVS8$assim[EVS8$assim==0] <- 0
EVS8$assim[EVS8$assim>=1 & EVS8$assim<=2] <- 1
EVS8$assim[EVS8$assim>=3 & EVS8$assim<=5] <- 2
EVS8$assim[EVS8$assim>=6 & EVS8$assim<=7] <- 3
EVS8$assim[EVS8$assim>=8 & EVS8$assim<=9] <- 4

EVS8[,'gayneigh']<- car::recode(EVS8[,'gayneigh'], "'multiple answers Mail' = NA;'no follow-up' = NA;'follow-up non response' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'mentioned' = 1; 'not mentioned' = 0")

EVS8[,'womenjobs']<- car::recode(EVS8[,'womenjobs'], "'disagree' = 1;'neither' = 2; 'agree' = 3")

#reversing
EVS8$weed <- abs(EVS8$weed - 10) 
EVS8$gay <- abs(EVS8$gay - 10)  
EVS8$abortion <- abs(EVS8$abortion - 10)
EVS8$divorce <- abs(EVS8$divorce - 10)
EVS8$euth <- abs(EVS8$euth - 10)
EVS8$sex <- abs(EVS8$sex - 10)

#recoding to five categories
EVS8$weed[EVS8$weed==0] <- 0
EVS8$weed[EVS8$weed>=1 & EVS8$weed<=2] <- 1
EVS8$weed[EVS8$weed>=3 & EVS8$weed<=5] <- 2
EVS8$weed[EVS8$weed>=6 & EVS8$weed<=7] <- 3
EVS8$weed[EVS8$weed>=8 & EVS8$weed<=9] <- 4

EVS8$gay[EVS8$gay==0] <- 0
EVS8$gay[EVS8$gay>=1 & EVS8$gay<=2] <- 1
EVS8$gay[EVS8$gay>=3 & EVS8$gay<=5] <- 2
EVS8$gay[EVS8$gay>=6 & EVS8$gay<=7] <- 3
EVS8$gay[EVS8$gay>=8 & EVS8$gay<=9] <- 4

EVS8$abortion[EVS8$abortion==0] <- 0
EVS8$abortion[EVS8$abortion>=1 & EVS8$abortion<=2] <- 1
EVS8$abortion[EVS8$abortion>=3 & EVS8$abortion<=5] <- 2
EVS8$abortion[EVS8$abortion>=6 & EVS8$abortion<=7] <- 3
EVS8$abortion[EVS8$abortion>=8 & EVS8$abortion<=9] <- 4

EVS8$divorce[EVS8$divorce==0] <- 0
EVS8$divorce[EVS8$divorce>=1 & EVS8$divorce<=2] <- 1
EVS8$divorce[EVS8$divorce>=3 & EVS8$divorce<=5] <- 2
EVS8$divorce[EVS8$divorce>=6 & EVS8$divorce<=7] <- 3
EVS8$divorce[EVS8$divorce>=8 & EVS8$divorce<=9] <- 4

EVS8$euth[EVS8$euth==0] <- 0
EVS8$euth[EVS8$euth>=1 & EVS8$euth<=2] <- 1
EVS8$euth[EVS8$euth>=3 & EVS8$euth<=5] <- 2
EVS8$euth[EVS8$euth>=6 & EVS8$euth<=7] <- 3
EVS8$euth[EVS8$euth>=8 & EVS8$euth<=9] <- 4

EVS8$sex[EVS8$sex==0] <- 0
EVS8$sex[EVS8$sex>=1 & EVS8$sex<=2] <- 1
EVS8$sex[EVS8$sex>=3 & EVS8$sex<=5] <- 2
EVS8$sex[EVS8$sex>=6 & EVS8$sex<=7] <- 3
EVS8$sex[EVS8$sex>=8 & EVS8$sex<=9] <- 4

EVS8[,'relig']<- car::recode(EVS8[,'relig'], "'never, practically never' = 1;'less often' = 2;'once a year' = 3; 'only on specific holy days' = 4; 'once a month' = 5; 'once a week' = 6; 'more than once week' = 7; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'multiple answers Mail' = NA")

EVS8[,'married']<- car::recode(EVS8[,'married'], "'married' = 'married';'registered partnership' = 'married';'widowed' = 'previously'; 'divorced' = 'previously'; 'separated' = 'previously'; 'never married or registered partnership' = 'never'; 'multiple answers Mail' = NA; 'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA")

EVS8$educ <- as.numeric(EVS8$educ)
EVS8$educ[EVS8$educ>=6 & EVS8$educ<=11] <- 'Less than HS'
EVS8$educ[EVS8$educ>=12 & EVS8$educ<=14] <- 'High school'
EVS8$educ[EVS8$educ>=16 & EVS8$educ<=18 | EVS8$educ== 23] <- 'Some college'
EVS8$educ[EVS8$educ>=19 & EVS8$educ<=22] <- 'College'
EVS8$educ[EVS8$educ>=24 & EVS8$educ<=25] <- 'Advanced'

EVS8[,'interest']<- car::recode(EVS8[,'interest'], "'na (survey break-off)' = NA; 'no answer' = NA; 'dont know' = NA; 'multiple answers Mail' = NA; 'very interested' = 4; 'somewhat interested' = 3; 'not very interested' = 2; 'not at all interested' = 1; 'follow-up non response' = NA; 'no follow-up' = NA")

EVS8$income <- as.numeric(EVS8$income)
#previous command makes variable range from 6-17, but should range from 1-12
EVS8$income <- EVS8$income - 5 
#was not measured as country deciles, like in 2017 wave, so creating deciles by country
EVS8 <- EVS8 %>% dplyr::group_by(country) %>% mutate(income = ntile(income, 10))
#need to ungroup to combine with other dataframe later
EVS8 <- ungroup(EVS8)

#in this wave, respondents were asked which party they would vote for in the next election, and if they declined to answer, they were asked which party appealed to them most; therefore, respondents with NAs on first variable may have provided a response to second question, and those who responded to first question have NAs on second, thus it is possible to combine columns
EVS8$party <- apply(EVS8[, 16:17], 1, max, na.rm = T)
#removing second, unneeded column
EVS8 <- EVS8[,-(17)]

EVS8$cohort <- NA
EVS8$cohort <- ifelse(EVS8$birth <=1939, 1, EVS8$cohort)
EVS8$cohort <- ifelse(EVS8$birth >=1940 & EVS8$birth <=1949, 2, EVS8$cohort)
EVS8$cohort <- ifelse(EVS8$birth >=1950 & EVS8$birth <=1959, 3, EVS8$cohort)
EVS8$cohort <- ifelse(EVS8$birth >=1960 & EVS8$birth <=1969, 4, EVS8$cohort)
EVS8$cohort <- ifelse(EVS8$birth >=1970 & EVS8$birth <=1979, 5, EVS8$cohort)
EVS8$cohort <- ifelse(EVS8$birth >=1980, 6, EVS8$cohort)
EVS8$cohort <- ifelse(EVS8$birth >=1990, 7, EVS8$cohort)

####
#creating indicator for year and then combining
EVS17$year <- 1
EVS8$year <- 0
EVSfull <- rbind(EVS17, EVS8)

#deleting few observations without gender or birth year
EVSfull <- subset(EVSfull, !is.na(birth) & !is.na(gender))

#deleting observations with high degree of missingness
delete.na <- function(DF, n=0) {
  DF[rowSums(is.na(DF)) <= n,]
}
EVSfull <- delete.na(EVSfull, 15)

#ensuring that variables (except country, party, gender, married, educ) are numeric
EVSfull[,c(2:15, 17, 22:27)] %<>% mutate_if(is.factor,as.numeric)
EVSfull[,c(2:15, 17, 22:27)] %<>% mutate_if(is.character,as.numeric)
EVSfull$educ <- as.factor(EVSfull$educ)
EVSfull$gender <- as.factor(EVSfull$gender)
EVSfull$married <- as.factor(EVSfull$married)

EVSfull$party[EVSfull$party == 'dont know' | EVSfull$party == 'no answer' | EVSfull$party == 'not applicable' | EVSfull$party == 'no follow-up' | EVSfull$party == 'other missing' | EVSfull$party == 'na (survey break-off)' |EVSfull$party == 'follow-up non response' ] <- NA

######
#now we will impute missing values using mice

#initializing imputation
ini <- mice(EVSfull, maxit = 0, print = T)

#pulling out imputation method for each column; should be pmm for all variables in need of imputation and "" otherwise (no missingness in country, gender, birth, cohort, year; weights and party do not need imputation)
meth <- ini$meth
meth 
meth[c('educ', 'married')] <- "pmm" #adjust variables here if needed
meth[c('party')] <- ""
#check again
meth

#pulling out predictor matrix (i.e., which variables will be used in imputation)
pred <- ini$predictorMatrix
#all columns used in imputation will have 1s
pred
#do not want to use these variables
pred[, c('party', 'pweight', 'gweight')] <- 0
pred
#will go through five iterations; should take around 15 minutes or less
EVSimp <- mice(EVSfull, meth = meth, pred = pred, print = T, nnet.MaxNWts = 5000)

#optional step
#saveRDS(imp, 'impEVScee.RDS')

#EVSimp <- readRDS('impEVScee.RDS')
EVSfull <- mice::complete(EVSimp)

######
EVSfull$party <- as.character(EVSfull$party)

EVSfull$party <- ifelse(EVSfull$party == 'CZ: No [no other] party appeals to me (spontaneous)'| EVSfull$party == 'EE: No [no other] party appeals to me (spontaneous)' | EVSfull$party == 'HU: No [no other] party appeals to me (spontaneous)	19'| EVSfull$party == ' LT: No [, no other] party appeals to me'| EVSfull$party == 'PL: No (no other) party appeals to me' | EVSfull$party == ' RO: No [no other] party appeals to me' | EVSfull$party =='SK: No [no other] party appeals to me (spontaneous)', NA, EVSfull$party)

parties <- plyr::count(EVSfull$party)
parties$parfam <- NA
colnames(parties)[1] <- 'party'

#first classified parties by CHES party families, but did not end up using
#lithuania
parties[9,3] <- 'socialist'
parties[2,3] <- 'conservative'
parties[6,3] <- 'agrarian'
parties[1,3] <- 'conservative'
parties[11,3] <- 'conservative'
parties[3,3] <- 'liberal'
parties[4,3] <- 'liberal'
parties[7,3] <- 'liberal'
parties[8,3] <- 'green'
parties[193,3] <- 'conservative'
parties[182,3] <- 'conservative'
parties[183,3] <- 'liberal'
parties[184,3] <- 'liberal'
parties[185,3] <- 'liberal'
parties[186,3] <- 'agrarian'
parties[188,3] <- 'conservative'
parties[190,3] <- 'socialist'
parties[191,3] <- 'liberal'
parties[194,3] <- 'liberal'
parties[5,3] <- 'liberal'

#romania
parties[20, 3] <- 'socialist'
parties[16, 3] <- 'radical right'
parties[17, 3] <- 'liberal'
parties[19, 3] <- 'christian democratic'
parties[15, 3] <- 'regionalist'
parties[13, 3] <- 'liberal'
parties[14, 3] <- 'liberal'
parties[219,3] <- 'liberal'
parties[220,3] <- 'liberal'
parties[221,3] <- 'conservative'
parties[222,3] <- 'christian democratic'
parties[223,3] <- 'liberal'
parties[224,3] <- 'radical right'
parties[225,3] <- 'radical right'
parties[226,3] <- 'socialist'
parties[227,3] <- 'regionalist'

#slovenia
parties[21, 3] <- 'no family'
parties[22, 3] <- 'christian democratic'
parties[23, 3] <- 'socialist'
parties[24, 3] <- 'socialist'
parties[25, 3] <- 'conservative'
parties[26, 3] <- 'christian democratic'
parties[27, 3] <- 'liberal'
parties[28, 3] <- 'radical right'
parties[29, 3] <- 'no family'
parties[30, 3] <- 'socialist'
parties[228,3] <- 'no family'
parties[229,3] <- 'liberal'
parties[230,3] <- 'radical right'
parties[231,3] <- 'christian democratic'
parties[234,3] <- 'conservative'
parties[235,3] <- 'christian democratic'
parties[236,3] <- 'radical right'
parties[237,3] <- 'socialist'

#bulgaria
parties[60,3] <- 'conservative'
parties[61,3] <- 'conservative'
parties[37,3] <- 'socialist'
parties[38,3] <- 'socialist'
parties[50,3] <- 'liberal'
parties[51,3] <- 'liberal'
parties[62,3] <- 'conservative'
parties[54,3] <- 'radical right'
parties[33,3] <- 'radical right'
parties[43,3] <- 'conservative'
parties[34,3] <- 'agrarian'
parties[35,3] <- 'agrarian'
parties[40,3] <- 'conservative'
parties[31,3] <- 'no family'
parties[32,3] <- 'socialist'
parties[36,3] <- 'socialist'
parties[41,3] <- 'radical left'
parties[42,3] <- 'conservative'
parties[47,3] <- 'green'
parties[53,3] <- 'liberal'
parties[57,3] <- 'green'
parties[58,3] <- 'radical right'
parties[39,3] <- 'green'

#czechia
parties[65,3] <- 'conservative'
parties[67,3] <- 'christian democratic'
parties[68,3] <- 'conservative'
parties[70,3] <- 'radical left'
parties[71,3] <- 'socialist'
parties[75,3] <- 'socialist'
parties[76,3] <- 'radical right'
parties[77,3] <- 'conservative'
parties[79,3] <- 'conservative'
parties[80,3] <- 'green'
parties[84,3] <- 'no family'
parties[85,3] <- 'christian democratic'
parties[86,3] <- 'radical left'
parties[91,3] <- 'conservative'
parties[93,3] <- 'conservative'
parties[94,3] <- 'green'
parties[95,3] <- 'conservative'
parties[96,3] <- 'radical right'
parties[97,3] <- 'socialist'
parties[98,3] <- 'no family'
parties[99,3] <- 'liberal'
parties[100,3] <- 'green'
parties[101,3] <- 'conservative'
parties[102,3] <- 'radical right'
parties[83,3] <- 'radical right'

#estonia
parties[103,3] <- 'liberal'
parties[104,3] <- 'liberal'
parties[105,3] <- 'christian democratic'
parties[106,3] <- 'radical right'
parties[107,3] <- 'regionalist'
parties[108,3] <- 'conservative'
parties[109,3] <- 'green'
parties[110,3] <- 'green'
parties[111,3] <- 'radical right'
parties[113,3] <- 'agrarian'
parties[114,3] <- 'liberal'
parties[115,3] <- 'socialist'
parties[118,3] <- 'conservative'
parties[119,3] <- 'socialist'
parties[120,3] <- 'socialist'
parties[121,3] <- 'conservative'

#croatia
parties[122,3] <- 'socialist'
parties[123,3] <- 'liberal'
parties[124,3] <- 'regionalist'
parties[125,3] <- 'regionalist'
parties[126,3] <- 'christian democratic'
parties[127,3] <- 'christian democratic'
parties[129,3] <- 'no family'
parties[130,3] <- 'no family'
parties[131,3] <- 'conservative'
parties[132,3] <- 'conservative'
parties[133,3] <- 'agrarian'
parties[134,3] <- 'agrarian'
parties[135,3] <- 'liberal'
parties[136,3] <- 'liberal'
parties[137,3] <- 'liberal'
parties[138,3] <- 'liberal'
parties[140,3] <- 'green'
parties[141,3] <- 'green'
parties[142,3] <- 'green'
parties[143,3] <- 'no family'
parties[146,3] <- 'regionalist'
parties[147,3] <- 'regionalist'
parties[152,3] <- 'liberal'
parties[153,3] <- 'socialist'
parties[154,3] <- 'socialist'
parties[155,3] <- 'socialist'

#hungary
parties[156,3] <- 'no family'
parties[157,3] <- 'christian democratic'
parties[158,3] <- 'socialist'
parties[159,3] <- 'green'
parties[160,3] <- 'conservative'
parties[161,3] <- 'conservative'
parties[162,3] <- 'agrarian'
parties[163,3] <- 'liberal'
parties[165,3] <- 'no family'
parties[164,3] <- 'socialist'
parties[166,3] <- 'radical left'
parties[167,3] <- 'radical right'
parties[168,3] <- 'christian democratic'
parties[169,3] <- 'conservative'
parties[170,3] <- 'radical right'
parties[171,3] <- 'radical left'
parties[172,3] <- 'socialist'
parties[173,3] <- 'liberal'
parties[174,3] <- 'radical right'
parties[178,3] <- 'green'
parties[179,3] <- 'liberal'
parties[180,3] <- 'liberal'

#poland
parties[195,3] <- 'christian democratic'
parties[196,3] <- 'christian democratic'
parties[197,3] <- 'socialist'
parties[198,3] <- 'liberal'
parties[199,3] <- 'radical right'
parties[200,3] <- 'radical right'
parties[201,3] <- 'radical right'
parties[202,3] <- 'confessional'
parties[203,3] <- 'conservative'
parties[204,3] <- 'liberal'
parties[205,3] <- 'no family'
parties[208,3] <- 'green'
parties[209,3] <- 'socialist'
parties[210,3] <- 'agrarian'
parties[211,3] <- 'agrarian'
parties[212,3] <- 'agrarian'
parties[213,3] <- 'socialist'
parties[214,3] <- 'conservative'
parties[215,3] <- 'socialist'
parties[216,3] <- 'liberal'
parties[217,3] <- 'socialist'

#slovakia
parties[239,3] <- 'liberal'
parties[240,3] <- 'conservative'
parties[241,3] <- 'radical left'
parties[242,3] <- 'socialist'
parties[243,3] <- 'liberal'
parties[244,3] <- 'radical left'
parties[245,3] <- 'radical right'
parties[246,3] <- 'conservative'
parties[247,3] <- 'no family'
parties[248,3] <- 'conservative'
parties[249,3] <- 'conservative'
parties[250,3] <- 'conservative'
parties[252,3] <- 'liberal'
parties[253,3] <- 'green'
parties[254,3] <- 'radical right'
parties[255,3] <- 'christian democratic'
parties[256,3] <- 'radical right'
parties[258,3] <- 'regionalist'
parties[259,3] <- 'regionalist'
parties[249,3] <- 'no family'
parties[260,3] <- 'radical right'

parties <- parties[,-2]
EVSeast <- merge(EVSfull, parties, by = 'party')
EVSeast$parfam <- ifelse(EVSeast$parfam == 'confessional', 'radical right', EVSeast$parfam)

#...then decided to group into GAL or TAN based on party families; this method ended up mischaracterizing some parties, so created final measure based on actual GAL-TAN scores
EVSeast$gt <- ifelse(EVSeast$parfam == 'christian democratic' | EVSeast$parfam == 'conservative' | EVSeast$parfam == 'radical right', 'TAN', 'GAL')
EVSeast$gt <- ifelse(EVSeast$parfam == 'no family' | EVSeast$parfam == 'regionalist' | EVSeast$parfam == 'agrarian', NA, EVSeast$gt)

fams <- EVSeast %>% group_by(parfam, gt) %>% dplyr::count(party)
#getting rid of the original gt column
EVSeast <- EVSeast[,-29]
#agrarian
fams[1,2] <- 'Center'
fams[4,2] <- 'TAN'
fams[5,2] <- 'TAN'
fams[6,2] <- 'TAN'
fams[8,2] <- 'TAN'
fams[9,2] <- 'TAN'
fams[10,2] <- 'TAN'
fams[11,2] <- 'TAN'

#CD
fams[12,2] <- 'Center'
fams[22,2] <- 'Center'
fams[23,2] <- 'Center'
fams[24,2] <- 'Center'
fams[27,2] <- 'Center'

#conservative
fams[32,2] <- 'Center'
fams[33,2] <- 'Center'
fams[34,2] <- 'Center'
fams[38,2] <- 'Center'
fams[39,2] <- 'Center'
fams[40,2] <- 'GAL'
fams[42,2] <- 'Center'
fams[44,2] <- 'Center'
fams[45,2] <- 'Center'
fams[46,2] <- 'Center'
fams[47,2] <- 'Center'
fams[48,2] <- 'Center'
fams[53,2] <- 'Center'
fams[63,2] <- 'Center'

#green
fams[72,2] <- 'Center'
fams[73,2] <- 'Center'

#liberal
fams[81,2] <- 'Center'
fams[82,2] <- 'Center'
fams[83,2] <- 'Center'
fams[84,2] <- 'Center'
fams[85,2] <- 'Center'
fams[86,2] <- 'Center'
fams[87,2] <- 'Center'
fams[88,2] <- 'Center'
fams[89,2] <- 'Center'
fams[90,2] <- 'Center'
fams[91,2] <- 'Center'
fams[92,2] <- 'Center'
fams[93,2] <- 'Center'
fams[94,2] <- 'Center'
fams[95,2] <- 'Center'
fams[96,2] <- 'Center'
fams[99,2] <- 'Center'
fams[100,2] <- 'Center'
fams[106,2] <- 'Center'
fams[107,2] <- 'Center'
fams[108,2] <- 'Center'
fams[109,2] <- 'Center'
fams[110,2] <- 'Center'
fams[111,2] <- 'Center'
fams[113,2] <- 'Center'
fams[114,2] <- 'Center'
fams[115,2] <- 'Center'
fams[116,2] <- 'Center'
fams[118,2] <- 'Center'

#none
fams[121,2] <- 'Center'
fams[122,2] <- 'Center'
fams[123,2] <- 'Center'
fams[124,2] <- 'Center'
fams[125,2] <- 'Center'
fams[128,2] <- 'GAL'
fams[129,2] <- 'Center'
fams[132,2] <- 'Center'
fams[133,2] <- 'TAN'
fams[134,2] <- 'TAN'

#radical left
fams[135,2] <- NA
fams[136,2] <- 'TAN'
fams[137,2] <- 'TAN'
fams[138,2] <- NA
fams[139,2] <- NA
fams[140,2] <- 'Center'
fams[141,2] <- 'Center'

#regionalist
fams[168,2] <- 'Center'
fams[170,2] <- 'TAN'
fams[171,2] <- 'TAN'
fams[172,2] <- 'GAL'
fams[173,2] <- 'GAL'
fams[174,2] <- 'Center'
fams[175,2] <- 'TAN'
fams[176,2] <- 'Center'

#socialist/communist
fams[177,2] <- 'Center'
fams[178,2] <- 'TAN'
fams[179,2] <- 'Center'
fams[180,2] <- 'Center'
fams[182,2] <- 'Center'
fams[183,2] <- NA
fams[184,2] <- 'Center'
fams[185,2] <- 'Center'
fams[186,2] <- 'Center'
fams[187,2] <- 'Center'
fams[188,2] <- 'Center'
fams[190,2] <- NA
fams[192,2] <- NA
fams[195,2] <- NA
fams[197,2] <- 'Center'
fams[198,2] <- 'Center'
fams[199,2] <- 'Center'
fams[102,2] <- NA
fams[207,2] <- 'TAN'

fams <- fams %>% arrange(party)

fams$gt2 <- fams$gt
#lithuania
fams[9, 5] <- 'GAL'
fams[190, 5] <- 'GAL'
fams[182, 5] <- 'Center'
fams[2, 5] <- 'Center'
fams[6, 5] <- 'TAN'
fams[4, 5] <- 'GAL'
fams[185, 5] <- 'GAL'
fams[5, 5] <- 'TAN'
#romania
fams[19, 5] <- 'TAN'
fams[14, 5] <- 'GAL'
#slovenia
fams[24, 5] <- 'GAL'
#bulgaria
fams[37, 5] <- 'TAN'
fams[38, 5] <- 'TAN'
fams[52, 5] <- 'TAN'
fams[63, 5] <- 'GAL'
fams[48, 5] <- 'TAN'
fams[41, 5] <- 'TAN'
fams[36, 5] <- 'Center'
#czechia
fams[68, 5] <- 'TAN'
fams[91, 5] <- 'TAN'
#estonia
fams[114, 5] <- 'GAL'
fams[118, 5] <- 'TAN'
fams[121, 5] <- 'TAN'
#croatia
fams[133, 5] <- 'Center'
fams[134, 5] <- 'Center'
fams[129, 5] <- 'Center'
fams[130, 5] <- 'Center'
fams[144, 5] <- 'GAL'
fams[145, 5] <- 'GAL'
fams[123, 5] <- 'TAN'
fams[143, 5] <- 'Center'
fams[122, 5] <- 'Center'
fams[137, 5] <- 'GAL'
fams[138, 5] <- 'GAL'
#hungary
fams[166, 5] <- 'TAN'
fams[171, 5] <- 'TAN'
fams[165, 5] <- 'GAL'
fams[164, 5] <- 'GAL'
fams[172, 5] <- 'GAL'
#poland
fams[197, 5] <- 'Center'
fams[214, 5] <- 'Center'
#slovakia
fams[257, 5] <- 'TAN'
fams[250, 5] <- 'Center'
fams[249, 5] <- 'GAL'

fams <- fams[,-c(1, 4)]
EVSeast <- merge(EVSeast, fams, by = 'party')

#######
#creating dummies and combining advanced and college categories, due to limited number of advanced educated
EVSeast$educ2 <- ifelse(EVSeast$educ == 'Advanced', 'College', as.character(EVSeast$educ))
EVSeast$educ2 <- as.factor(EVSeast$educ2)
EVSeast$hs <- ifelse(EVSeast$educ == 'High school', 1, 0)
EVSeast$lesshs <- ifelse(EVSeast$educ == 'Less than HS', 1, 0)
EVSeast$somecol <- ifelse(EVSeast$educ == 'Some college', 1, 0)
EVSeast$college <- ifelse(EVSeast$educ2 == 'College', 1, 0)
EVSeast$gender <- as.numeric(EVSeast$gender)

EVSeast$educ <- factor(EVSeast$educ, levels = c('Less than HS', 'High school', 'Some college', 'College', 'Advanced'))
EVSeast$educnum <- as.numeric(EVSeast$educ)

#END OF DATA CLEANING
#############
#BEGIN ANALYSES
#############

###### Figure 4 - party frequency
EVSgt <- subset(EVSeast, !is.na(gt2))
EVSw <- svydesign(ids = ~1, data = EVSgt, weights = EVSgt$pweight*EVSgt$gweight)
n3 <- svytable(~gt2 + year, design = EVSw)
n3 <- as.data.frame(n3)

n4 <- svytotal(~as.factor(year), design = EVSw)
n4 <- as.data.frame(n4)
n4 <- n4[,-2]
n4 <- as.data.frame(n4)
n4$year <- c(0,1)
plot3 <- merge(n3, n4, by = "year")
plot3$per <- plot3$Freq/plot3$n4 * 100
plot3$gt2 <- factor(plot3$gt2, levels = c('GAL', 'Center', 'TAN'))

plot2 <- ggplot() + geom_bar(aes(y = per, x = gt2, fill = year), data = plot3, position="dodge", stat = 'identity') + scale_fill_manual(name="Year", labels=c("2008", "2017"), values = c("grey18","grey70")) + labs(x = "Party Type", y = "Percentage") + theme_bw() + ylim(0, 60)

######
EVSeast$educyear <- paste(EVSeast$educ2, EVSeast$year)
EVSeast$educyear <- as.factor(EVSeast$educyear)
EVSeast$educyear <- relevel(EVSeast$educyear, ref = 'High school 0')
EVSeast <- EVSeast %>% arrange(educyear)

#Model corresponds to Figure 5 in main text and Table 8 in Appendix B
sem1d <- 'im =~ imjobs + imtakejobs + imcrime + imwelfare + imneigh + assim 
im ~ income + relig + cohort 
'

semfiteduc <- sem(sem1d, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim' ,'imneigh', 'income', 'relig', 'cohort', 'gender'), group = 'educyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

summary(semfiteduc, standardized = T, fit.measures = T, rsquare = T) 

semfiteducres <- standardizedsolution(semfiteduc) 
semfiteducres <- subset(semfiteducres, op == '~' | op == '~1')
semfiteducres$year <- ifelse(semfiteducres$group == 1 | semfiteducres$group == 2 | semfiteducres$group == 5 | semfiteducres$group == 7, 2008, 2017)
semfiteducres$year <- as.factor(semfiteducres$year)
semfiteducres$lrs <- ifelse(semfiteducres$group == 1 | semfiteducres$group == 4, 'High school', NA)
semfiteducres$lrs <- ifelse(semfiteducres$group == 2 | semfiteducres$group == 3, 'College', semfiteducres$lrs)
semfiteducres$lrs <- ifelse(semfiteducres$group == 5 | semfiteducres$group == 6, 'Less than HS', semfiteducres$lrs)
semfiteducres$lrs <- ifelse(semfiteducres$group == 7 | semfiteducres$group == 8, 'Some college', semfiteducres$lrs)
semfiteducres$lrs <- factor(semfiteducres$lrs, levels = c('Less than HS', 'High school', 'Some college', 'College'))

semfiteducres1 <- subset(semfiteducres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')

educplot2 <- subset(semfiteducres1, op == '~1')
educplot2$lhs <- as.factor(educplot2$lhs)
educplot2 <- subset(educplot2, lhs == 'im')

p10 <- ggplot(educplot2) + geom_point(aes(lrs, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Education and Year', x = '', color = 'Year') + geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5, 1.15)+ geom_linerange(aes(x = lrs, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()+theme(axis.text=element_text(size=6.75))

####### unconstrained version of model (see Appendix B)
semfiteducun <- sem(sem1d, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','gayparents', 'duty', 'stayhome','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'educyear', cluster = 'country')

summary(semfiteducun, standardized = T, fit.measures = T, rsquare = T)
#######
EVSeast <- EVSeast %>% arrange(year) 

#Model 1 in Table 1
sem1a <- 'trad =~ gayneigh + weed + gay + abortion + divorce + euth + sex + womenjobs
im =~ imjobs + imtakejobs + imcrime + imwelfare + imneigh + assim
im ~ trad
'

semfit1a <- sem(sem1a, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'year', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitsa <- standardizedsolution(semfit1a) 
#Model 1, Table 1 results
semfitsa <- subset(semfitsa, op == '~' | op == '~1')

##### unconstrained version of model (see Appendix B)
semfit1aun <- sem(sem1a, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','gayparents', 'duty', 'stayhome','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'year', cluster = 'country')
summary(semfit1aun, standardized = T, fit.measures = T, rsquare = T) 
######
#Model 2, Table 1
sem1b <- 'trad =~ gayneigh + weed+ gay + abortion + divorce + euth + sex + womenjobs
im =~ imjobs + imtakejobs + imcrime + imwelfare + imneigh + assim
im ~ trad + gender + income + relig + cohort + lesshs + college + somecol
'

semfit1b <- sem(sem1b, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'year', cluster = 'country', group.equal = c('loadings', 'intercepts'))

summary(semfit1b, standardized = T, fit.measures = T, rsquare = T) 

semfits <- standardizedsolution(semfit1b) 
#Model 2, Table 1 results
semfits <- subset(semfits, op == '~' | op == '~1')

semfits$year <- ifelse(semfits$group == 1, 2008, 2017)
semfits$year <- as.factor(semfits$year)

semfits1 <- subset(semfits, lhs == 'im' | lhs == 'trad' & rhs == 'im' | lhs == 'trad' & op == '~1')

plot2 <- subset(semfits1, op == '~1')
plot2$lhs <- as.factor(plot2$lhs)
plot2$lhs <- ifelse(plot2$lhs == 'trad', 'Traditionalism', 'Immigration')

#Figure 6
p2 <- ggplot(plot2) + geom_point(aes(year, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = year, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw() + ylim(-1.5,1.15)

##### unconstrained version of model (see Appendix B)
semfit1bun <- sem(sem1b, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','gayparents', 'duty', 'stayhome','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'year', cluster = 'country')
summary(semfit1bun, standardized = T, fit.measures = T, rsquare = T)

#####
EVSgt <- subset(EVSeast, !is.na(gt2))
#creating indicator for party type-year grouping
EVSgt$gtyear <- paste(EVSgt$gt2, EVSgt$year)
EVSgt$gtyear <- as.factor(EVSgt$gtyear)

EVSgt <- EVSgt %>% arrange(gtyear)  

#Model corresponding to Figures 7 and 8 in main text and Table 9 in Appendix B
semfitgt <- sem(sem1b, data = EVSgt, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

summary(semfitgt, standardized = T, fit.measures = T, rsquare = T) 

semfitgtres <- standardizedsolution(semfitgt) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')

semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

#Figure 7
p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

#Figure 8
p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.35)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw() 

##### unconstrained version of model
semfitrlun <- sem(sem1b, data = EVSgt, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country')
summary(semfitrlun, standardized = T, fit.measures = T, rsquare = T) 

#####
#creating indicator for political interest-party type-year grouping
EVSgt$int2 <- ifelse(EVSgt$interest < 3, 0, 1)
EVSgt$gtint <- paste(EVSgt$int2, EVSgt$gt2, EVSgt$year)
EVSgt$gtint <- as.factor(EVSgt$gtint)
EVSgt <- EVSgt %>% arrange(gtint)

#need to use numeric version of education variable instead of dummies; groups becoming smaller
sem1d <- 'trad =~ gayneigh + weed +gay + abortion + divorce + euth + sex + womenjobs
im =~ imjobs + imtakejobs + imcrime + imwelfare + imneigh + assim 
im ~ trad + income + relig + gender + cohort + educnum
'

#Model corresponds to Figure 9 in main text and Table 10 in Appendix B
semfitgtint <- sem(sem1b, data = EVSgt, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','gayparents', 'duty', 'stayhome','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtint', cluster = 'country', group.equal = c('loadings', 'intercepts'))

summary(semfitgtint, standardized = T, fit.measures = T, rsquare = T) 

semfitgtintres <- standardizedsolution(semfitgtint) 
semfitgtintres <- subset(semfitgtintres, op == '~' | op == '~1')
semfitgtintres$year <- ifelse(semfitgtintres$group %% 2 == 0, 2017, 2008)
semfitgtintres$year <- as.factor(semfitgtintres$year)
semfitgtintres$lrs <- ifelse(semfitgtintres$group == 1 | semfitgtintres$group == 2 | semfitgtintres$group == 7 | semfitgtintres$group == 8, 'Center', NA)
semfitgtintres$lrs <- ifelse(semfitgtintres$group == 3 | semfitgtintres$group == 4 | semfitgtintres$group == 9 | semfitgtintres$group == 10, 'GAL', semfitgtintres$lrs)
semfitgtintres$lrs <- ifelse(semfitgtintres$group == 5 | semfitgtintres$group == 6 | semfitgtintres$group == 11 | semfitgtintres$group == 12, 'TAN', semfitgtintres$lrs)

semfitgtintres$lrs <- factor(semfitgtintres$lrs, levels = c('GAL', 'Center', 'TAN'))

semfitgtintres$int <- ifelse(semfitgtintres$group < 7, "Uninterested", "Interested")
semfitgtintres$int <- as.factor(semfitgtintres$int)

semfitgtintres1 <- subset(semfitgtintres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')

gtintplot1 <- subset(semfitgtintres1, op == '~')

#Figure 9
p9 <- ggplot(gtintplot1) + geom_point(aes(lrs, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = lrs, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw() + facet_wrap(~int)

##### unconstrained version of model
semfitgtintun <- sem(sem1b, data = EVSgt, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','gayparents', 'duty', 'stayhome','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtint', cluster = 'country')

summary(semfitgtintun, standardized = T, fit.measures = T, rsquare = T) 

###########
#END OF MAIN ANALYSES

#BEGIN SUPPLEMENTAL ANALYSES
########## Figures 4 and 5 in Appendix C

EVSgt$year <- as.factor(EVSgt$year)
EVSgt$gt2 <- as.factor(EVSgt$gt2)

EVSgt$educ2 <- relevel(EVSgt$educ2, ref = 'High school')
EVSgt$educ2 <- factor(EVSgt$educ2, levels = c('High school', 'Less than HS', 'Some college', 'College'))
EVSgt$gal <- ifelse(EVSgt$gt2 == 'GAL', 1, 0)
g1 <- glmer(gal ~ educ2*year + relig + gender + married + income + cohort + (1 | country), data = EVSgt, family = binomial(link = 'logit'))
summary(g1)
pg1 <- plot_model(g1, terms = c('educ2Less than HS', 'educ2Less than HS:year1', 'educ2Some college', 'educ2Some college:year1','educ2College','educ2College:year1', 'year1'), colors = c('grey5', 'grey5'), order.terms = c(4, 1, 11, 2, 12, 3,13), axis.labels = c('College * 2017', 'College','Some college * 2017', 'Some college', 'Less than HS * 2017' ,'Less than HS', '2017'), title = 'GAL Preference by Year and Education') + theme_bw() + geom_hline(yintercept = 1, color = 'grey75', linetype = 'dashed') + ylim(.25, 2.25)

pg1 <- plot_model(g1, terms = c('educ2Less than HS', 'educ2Less than HS:year1', 'educ2Some college', 'educ2Some college:year1','educ2College','educ2College:year1', 'year1'), colors = c('grey5', 'grey5'), order.terms = c(4, 1, 5, 2, 6, 3, 7), axis.labels = c('College * 2017', 'College','Some college * 2017', 'Some college', 'Less than HS * 2017' ,'Less than HS', '2017'), title = 'GAL Preference by Year and Education') + theme_bw() + geom_hline(yintercept = 1, color = 'grey75', linetype = 'dashed') + ylim(.25, 2.25)

EVSgt$tan <- ifelse(EVSgt$gt2 == 'TAN', 1, 0)
g2 <- glmer(tan ~ educ2*year + relig + gender + married + income + cohort + (1 | country), data = EVSgt, family = binomial(link = 'logit'))
summary(g2)

pg2 <- plot_model(g2, terms = c('educ2Less than HS', 'educ2Less than HS:year1', 'educ2Some college', 'educ2Some college:year1','educ2College','educ2College:year1', 'year1'), colors = c('grey5', 'grey5'), order.terms = c(4, 1, 5, 2, 6, 3, 7), title = 'TAN Preference by Year and Education', axis.labels = c('College * 2017', 'College','Some college * 2017', 'Some college', 'Less than HS * 2017' ,'Less than HS', '2017')) + theme_bw() + geom_hline(yintercept = 1, color = 'grey75', linetype = 'dashed') + ylim(.25, 2.25)

######## country-year model; corresponds to Figures 6 and 7 in Appendix C
sem1d <- 'trad =~ gayneigh + weed + gay + abortion + divorce + euth + sex + womenjobs
im =~ imjobs + imtakejobs + imcrime + imwelfare + imneigh + assim 
trad ~ im
'
EVSeast$countryyear <- paste(EVSeast$country, EVSeast$year)
EVSeast$countryyear <- as.factor(EVSeast$countryyear)
EVSeast$countryyear <- relevel(EVSeast$countryyear, ref = 'Bulgaria 0')
EVSeast <- EVSeast %>% arrange(countryyear)

semfitc <- sem(sem1d, data = EVSeast, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'countryyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

summary(semfitc, standardized = T, fit.measures = T, rsquare = T) 

semfitcres <- standardizedsolution(semfitc) 
semfitcres <- subset(semfitcres, op == '~' | op == '~1')

semfitcres$year <- ifelse(semfitcres$group %% 2 == 0, 2017, 2008)
semfitcres$year <- as.factor(semfitcres$year)
semfitcres$country <- ifelse(semfitcres$group == 1 | semfitcres$group ==2,'Bulgaria', NA)
semfitcres$country <- ifelse(semfitcres$group == 3 | semfitcres$group == 4,'Croatia', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 5| semfitcres$group == 6,'Czechia', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 7 | semfitcres$group ==8,'Estonia', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 9 | semfitcres$group == 10,'Hungary', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 11 | semfitcres$group == 12,'Lithuania', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 13 | semfitcres$group == 14,'Poland', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 15 | semfitcres$group == 16,'Romania', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 17 | semfitcres$group == 18,'Slovakia', semfitcres$country)
semfitcres$country <- ifelse(semfitcres$group == 19 | semfitcres$group == 20,'Slovenia', semfitcres$country)

semfitcres1 <- subset(semfitcres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')

cplot1 <- subset(semfitcres1, op == '~')

#Appendix C Figure 7
p7 <- ggplot() + geom_point(aes(cplot1$country, cplot1$est.std, color = cplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = cplot1$country, ymin = cplot1$ci.lower, ymax = cplot1$ci.upper, color = cplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw() + theme(axis.text.x=element_text(size=8))
ggsave(p7, filename = "~/Desktop/p7.png", width = 6, height = 4, units = "in")

cplot2 <- subset(semfitcres1, op == '~1')
cplot2$lhs <- as.factor(cplot2$lhs)
cplot2$lhs <- ifelse(cplot2$lhs == 'im', 'Immigration', 'Traditionalism')

#Appendix C Figure 6
p8 <- ggplot(cplot2) + geom_point(aes(country, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Country and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-2,1.75) + geom_linerange(aes(x = country, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()+ theme(axis.text=element_text(size=6.75))

ggsave(p8, filename = "~/Desktop/p8.png", width = 9.5, height = 4, units = "in")

######## JACKNIFE MODELS (see Appendix C)

EVSgt1 <- subset(EVSgt, country != 'Bulgaria')

semfitgt1 <- sem(sem1b, data = EVSgt1, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt1) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

##
EVSgt2 <- subset(EVSgt, country != 'Croatia')

semfitgt2 <- sem(sem1b, data = EVSgt2, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt2) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

##
EVSgt3 <- subset(EVSgt, country != 'Czech Republic')

semfitgt3 <- sem(sem1b, data = EVSgt3, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt3) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

###
EVSgt4 <- subset(EVSgt, country != 'Estonia')

semfitgt4 <- sem(sem1b, data = EVSgt4, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt4) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

###
EVSgt5 <- subset(EVSgt, country != 'Hungary')

semfitgt5 <- sem(sem1b, data = EVSgt5, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt5) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

####
EVSgt6 <- subset(EVSgt, country != 'Lithuania')

semfitgt6 <- sem(sem1b, data = EVSgt6, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt6) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

####
EVSgt7 <- subset(EVSgt, country != 'Poland')

semfitgt7 <- sem(sem1b, data = EVSgt7, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt7) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

###
EVSgt8 <- subset(EVSgt, country != 'Romania')

semfitgt8 <- sem(sem1b, data = EVSgt8, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt8) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

###
EVSgt9 <- subset(EVSgt, country != 'Slovak Republic')

semfitgt9 <- sem(sem1b, data = EVSgt9, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt9) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

###
EVSgt10 <- subset(EVSgt, country != 'Slovenia')

semfitgt10 <- sem(sem1b, data = EVSgt10, ordered =  c('imjobs', 'imtakejobs', 'imcrime', 'imwelfare', 'assim', 'gayneigh', 'womenjobs','weed', 'gay', 'abortion' ,'divorce','euth', 'sex', 'imneigh'), group = 'gtyear', cluster = 'country', group.equal = c('loadings', 'intercepts'))

semfitgtres <- standardizedsolution(semfitgt10) 
semfitgtres <- subset(semfitgtres, op == '~' | op == '~1')
semfitgtres$year <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 3 | semfitgtres$group == 5, 2008, 2017)
semfitgtres$year <- as.factor(semfitgtres$year)
semfitgtres$gt <- ifelse(semfitgtres$group == 1 | semfitgtres$group == 2, 'Center', NA)
semfitgtres$gt <- ifelse(semfitgtres$group == 3 | semfitgtres$group == 4, 'GAL', semfitgtres$gt)
semfitgtres$gt <- ifelse(semfitgtres$group == 5 | semfitgtres$group == 6, 'TAN', semfitgtres$gt)
semfitgtres1 <- subset(semfitgtres, lhs == 'trad' | lhs == 'im' & rhs == 'trad' | lhs == 'im' & op == '~1')
semfitgtres1$gt <- factor(semfitgtres1$gt, levels = c('GAL', 'Center', 'TAN'))

gtplot1 <- subset(semfitgtres1, op == '~')

p5 <- ggplot() + geom_point(aes(gtplot1$gt, gtplot1$est.std, color = gtplot1$year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Traditionalism and Immigration Attitudes Correlation', x = '', color = 'Year') + ylim(-.15, .45)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + geom_linerange(aes(x = gtplot1$gt, ymin = gtplot1$ci.lower, ymax = gtplot1$ci.upper, color = gtplot1$year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

gtplot2 <- subset(semfitgtres1, op == '~1')
gtplot2$lhs <- as.factor(gtplot2$lhs)
gtplot2$lhs <- ifelse(gtplot2$lhs == 'trad', 'Traditionalism', 'Immigration')

p6 <- ggplot(gtplot2) + geom_point(aes(gt, est.std, color = year), size = 2.25, position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ labs(y = 'Mean Latent Attitude by Party Type and Year', x = '', color = 'Year') + facet_wrap(~lhs)+ geom_hline(yintercept = 0, color = 'grey75', linetype = 'dashed') + ylim(-1.5,1.5)+ geom_linerange(aes(x = gt, ymin = ci.lower, ymax = ci.upper, color = year), position=position_dodge(width=0.5))+ scale_color_manual(values = c('2008' = 'grey5','2017' = 'grey60'))+ theme_bw()

######### END OF SCRIPT